package com.gexin.model.system;

import com.gexin.common.cache.CacheUtils;
import com.gexin.common.cache.RedisKeyConst;
import com.gexin.common.utils.SQLUtils;
import com.gexin.common.utils.StringUtil;
import com.gexin.model.system.base.BaseAddrCities;
import com.jfinal.plugin.activerecord.Page;

import java.util.List;

/**
 * 业务名称:  t_addr_cities(行政区域地州市信息表) 
 */
@SuppressWarnings("serial")
public class AddrCities extends BaseAddrCities<AddrCities> {
	public static final AddrCities dao = new AddrCities();


	public static final Integer TYPE_OPEN_SUCCESS =1;
	public static final Integer TYPE_OPEN_FAIL =0;



	/**
	 * 根据cityid查询
	 * @param id
	 * @return
	 */
	public AddrCities findByCityId(String id) {
		return dao.findFirst("select * from t_addr_cities where cityid=?",id);
	}

	public List<AddrCities>  getCitiesByProvincesId(String province_id){
		SQLUtils sql =new SQLUtils("select *  from  t_addr_cities where 1=1 ");
		sql.whereEquals(FIELD_PROVINCEID, province_id);
		sql.append(" order by id asc ");
		return dao.find(sql.toString());
	}

	/**
	 * fang  2016-07-06
	 * 拿到所有的市
	 */
/*	public List<AddrCities> getAllCities(){
		String sql=" select *  from t_addr_cities order by id asc";
		return dao.find(sql);
	}
	*/
	/**
	 * fang  缓存拿到所有的市区
	 *
	 */
	public List<AddrCities>cache_getAllCities(){
		List<AddrCities>  city_list = CacheUtils.get(RedisKeyConst.Address_City);
		if(city_list==null){
			city_list=dao.find("select *  from t_addr_cities order by id asc");
			CacheUtils.set(RedisKeyConst.Address_Province, city_list,365*24*3600);//缓存一年
		}
		return city_list;
	}

	/**
	 * fang  2016-07-08
	 * 根据 province_id 拿到所有的市区
	 *
	 */
	public List<AddrCities> getCitiesByProvinceId(String province_id){
		SQLUtils sql =new SQLUtils("select *  from  t_addr_cities where 1=1 ");
		sql.whereEquals(FIELD_PROVINCEID, province_id);
		sql.append(" order by id asc ");
		return dao.find(sql.toString());
	}

	/**
	 * fang  2016-07-12
	 * 根据cityid  扎到city
	 * @param city_id
	 * @return
	 */
	public AddrCities getCityByCityId(String city_id){
		String sql=" select *  from t_addr_cities where cityid =?";
		return dao.findFirst(sql,city_id);
	}

	public Page<AddrCities> findPageArea(Integer page , Integer pageSize , String name) {
		SQLUtils sql = new SQLUtils();
		sql.append(" from t_addr_cities tac ");
		sql.append(" LEFT JOIN t_addr_provinces tap on tac.provinceid=tap.provinceid ");
		sql.append(" LEFT JOIN t_agent tagent on tac.agent_id=tagent.id ");
		sql.append("  where 1=1 ");
		if(!StringUtil.isBlank(name)){
			sql.whereLike("tac.city",name);
		}
		return paginate(page,pageSize,"select tac.*,tap.province,tagent.agent_name AS agentName ",sql.toString());
	}

	public List<AddrCities> findByProvinceId(String provinceid) {
		return	find("select * from t_addr_cities where provinceid=?",provinceid);
	}
}
